import sys
from PyQt6 import QtWidgets
from PyQt6.QtGui import QIcon
from main_window import Ui_MainWindow  # 导入由 UI 文件生成的类
from SocUpdate.soc_update_code import FileUploader  # 导入 FileUploader 类
from ToolchainsUpdate.tools_update_code import ToolUploader
from TimeSync.truly_car_timesync_code import CarTimeSyncApp
from ResourceAnalysis.resource_statistics_code import MonitoringDialog
from TrosParser.tros_parser_code import TrosParserDialog

class MainApp(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.resize(800, 600)  # 确保主窗口大小适当
        self.setWindowIcon(QIcon("icon/app.png"))

        self.ui = Ui_MainWindow()  # 创建 UI 类的实例
        self.ui.setupUi(self)  # 调用 setupUi 设置 UI 界面

        # 实例化 FileUploader 窗口
        self.file_uploader = FileUploader()  # 创建 FileUploader 的实例
        self.ui.stackedWidget.addWidget(self.file_uploader)
        self.ui.actionDzSocApps.triggered.connect(self.show_file_uploader)

        # 实例化 ToolUploader 窗口
        self.tool_uploader = ToolUploader()  # 创建 ToolUploader 的实例
        self.ui.stackedWidget.addWidget(self.tool_uploader)
        self.ui.actionToolChains.triggered.connect(self.show_toolchain_uploader)

        # 实例化 TrulyValueCar 窗口
        self.truly_car = CarTimeSyncApp()  # 创建 TrulyValueCar 的实例
        self.ui.stackedWidget.addWidget(self.truly_car)
        self.ui.actionTrulyValueCar.triggered.connect(self.show_truly_value_car)

        # 实例化 ResourceStatistics 窗口
        self.res_stat = MonitoringDialog()  # 创建 ResourceStatistics 的实例
        self.ui.stackedWidget.addWidget(self.res_stat)
        self.ui.actionResourceStatistics.triggered.connect(self.show_resource_statistics)

        # 实例化 TrosParserDialog 窗口
        self.tros_parser = TrosParserDialog()  # 创建 ResourceStatistics 的实例
        self.ui.stackedWidget.addWidget(self.tros_parser)
        self.ui.actionTrosParser.triggered.connect(self.show_tros_parser)

    def show_file_uploader(self):
        print("Show SOC update page!")
        self.ui.stackedWidget.setCurrentWidget(self.file_uploader)
        self.ui.stackedWidget.update()
        # 打印当前页面的名称
        current_widget = self.ui.stackedWidget.currentWidget()
        print(f"Current widget: {type(current_widget).__name__}")

    def show_toolchain_uploader(self):
        print("Show toolchains update page!")
        self.ui.stackedWidget.setCurrentWidget(self.tool_uploader)
        self.ui.stackedWidget.update()
        # 打印当前页面的名称
        current_widget = self.ui.stackedWidget.currentWidget()
        print(f"Current widget: {type(current_widget).__name__}")

    def show_truly_value_car(self):
        print("Show TrulyValueCar time sync page!")
        self.ui.stackedWidget.setCurrentWidget(self.truly_car)
        self.ui.stackedWidget.update()
        # 打印当前页面的名称
        current_widget = self.ui.stackedWidget.currentWidget()
        print(f"Current widget: {type(current_widget).__name__}")

    def show_resource_statistics(self):
        print("Show Resource Statistics page!")
        self.ui.stackedWidget.setCurrentWidget(self.res_stat)
        self.ui.stackedWidget.update()
        # 打印当前页面的名称
        current_widget = self.ui.stackedWidget.currentWidget()
        print(f"Current widget: {type(current_widget).__name__}")

    def show_tros_parser(self):
        print("Show Tros Parser page!")
        self.ui.stackedWidget.setCurrentWidget(self.tros_parser)
        self.ui.stackedWidget.update()
        # 打印当前页面的名称
        current_widget = self.ui.stackedWidget.currentWidget()
        print(f"Current widget: {type(current_widget).__name__}")

def main():
    app = QtWidgets.QApplication(sys.argv)  # 创建应用程序实例
    window = MainApp()  # 创建主窗口实例
    window.show()  # 显示窗口
    sys.exit(app.exec())  # 进入应用程序主循环


if __name__ == "__main__":
    main()
